/*******************************************************************************
 * Copyright 2010 MoBrite (A venture of Kinara Systems Private Limited)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.mobrite.domaindroid.activity;

import com.mobrite.domaindroid.R;
import com.mobrite.domaindroid.db.DomainObject;
import com.mobrite.domaindroid.util.Logger;
import com.mobrite.domaindroid.util.Util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class DomainListItem extends RelativeLayout {

	public long id = -1;	// Id in database
	
	public String domain;
	public String tld;
	//public int status;
	public String whoisResponse;
    public boolean mFavorite;
    boolean mSelected;
    public long timestamp;
    public boolean mAvailable;
    
    public int listPosition = -1;	// Position in ListView;
    
    //private boolean mAllowBatch;
    private boolean selectionPresent; 
    
	private DomainListItemViewCallback viewCallback;
	
	private boolean mCachedViewPositions;
    private boolean mDownEvent;
    private int mStarLeft;
    private int mCheckRight;
   
	// Padding to increase clickable areas on left & right of each list item
    //private final static float CHECKMARK_PAD = 10.0F;
    private final static float STAR_PAD = 10.0F;
    private final static float CHECKMARK_PAD = 10.0F;

	public DomainListItem(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public DomainListItem(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public DomainListItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

    /**
     * Called by the adapter at getView()/bindView() time
     * 
     * @param callback Callback that will receive updateFavorite/updateSelected notifications. 
     * This will typically be the adapter itself.
     */
    public void viewInit(DomainListItemViewCallback callback, boolean selectionPresent) {
        this.viewCallback = callback;
        mCachedViewPositions = false;
        //mAllowBatch = true;
        this.selectionPresent = selectionPresent;
    }

    /**
     * Overriding this method allows us to "catch" clicks in the checkbox or star
     * and process them accordingly.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = false;
        int touchX = (int) event.getX();

        if (!mCachedViewPositions) {
            float paddingScale = getContext().getResources().getDisplayMetrics().density;
            int starPadding = (int) ((STAR_PAD * paddingScale) + 0.5);
            int checkPadding = (int) ((CHECKMARK_PAD * paddingScale) + 0.5);
            mStarLeft = findViewById(R.id.favorite).getLeft() - starPadding;
            if (selectionPresent) {
            	mCheckRight = findViewById(R.id.selected).getRight() + checkPadding;
            }
            mCachedViewPositions = true;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownEvent = true;
                if ((selectionPresent && touchX < mCheckRight) || touchX > mStarLeft) {
                    handled = true;
                }
                break;

            case MotionEvent.ACTION_CANCEL:
                mDownEvent = false;
                break;

            case MotionEvent.ACTION_UP:
                if (mDownEvent) {
                	if (selectionPresent && touchX < mCheckRight) {
                        mSelected = !mSelected;
                        viewCallback.updateSelected(this, mSelected);
                        Logger.d("Selection Hit on "+domain);
                        handled = true;
                    } else if (touchX > mStarLeft) {
                        mFavorite = !mFavorite;
                        viewCallback.updateFavorite(this, mFavorite);
                        Logger.d("Favorite Star Hit on "+domain);
                        handled = true;
                    }
                }
                break;
        }

        if (handled) {
            postInvalidate();
        } else {
            handled = super.onTouchEvent(event);
        }

        return handled;
    }

    @Override
    public boolean equals(Object o) {
    	//return o instanceof DomainListItem && domain.equals(((DomainListItem)o).domain);
    	return o instanceof DomainListItem && id==((DomainListItem)o).id;
    }
    
    @Override
    public int hashCode() {
    	Logger.d("hashCode() called");
    	//return Long.valueOf(id).hashCode();
    	//return new String(domain+"."+tld).hashCode();
    	return 0;
    }
    
    @Override
    public String toString() {
    	return "id="+id+", domain="+domain+", tld="+tld+", selected="+mSelected;
    }
    
    public DomainObject getDomainObject() {
    	DomainObject dobj = new DomainObject(domain, tld, mAvailable ? 1 : 0, mFavorite ? 1 : 0, timestamp);
    	dobj.setRowId(id);
    	return dobj;
    }
}
